﻿// ----------------------------------------------------------------------
//
// <copyright file="IniFile.cs" company="TheAcidToy Inc" Date="2012">
//     TheAcidToy is a registered trademark of TheAcidToy Inc. All right reserved, copywrited: 2012
// </copyright>
//
// ------------------------------------------------------------------------
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System;
using System.Collections;

public class IniFile
{
    public string path;

    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

    public IniFile(string INIPath)
    {
        path = INIPath;
    }

    public void IniWriteValue(string Section, string Key, string Value)
    {
        WritePrivateProfileString(Section, Key, Value, this.path);
    }

    public string IniReadValue(string Section, string Key)
    {
        StringBuilder temp = new StringBuilder(255);
        int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
        return temp.ToString();
    }
}

public static class Load_ini
{
    static string m_path;
    public static bool Get_INI_Bool(string section, string Key)
    {
        IniFile ini = new IniFile(m_path);
        return Convert.ToBoolean(ini.IniReadValue(section, Key));
    }

    public static void make_INI(string path)
    {
        if (!File.Exists(path))
        {
            m_path = path;

            //File.Create(path);
            
            //IniFile ini = new IniFile(path);

            StreamWriter sw = new StreamWriter(path);
            //Client Loading
            sw.WriteLine("[Load]");
            sw.WriteLine("; Handles ");
            sw.WriteLine("Auto_Del=False");
            sw.WriteLine("Auto_kill=False");

            //Client Caching
            sw.WriteLine("");
            sw.WriteLine("[Cache]");
            sw.WriteLine("; Handles Auto saving of product files");
            sw.WriteLine("Auto_Cache=False");

            //Auto Cleaning
            sw.WriteLine("");
            sw.WriteLine("[Auto]");
            sw.WriteLine("; Handles Auto Cleanerto do it or not");
            sw.WriteLine("Auto_Clean=True");
            sw.WriteLine("; Handles Auto Cleaner Stored in days");
            sw.WriteLine("Auto_Clean_Timeout=3");
            sw.WriteLine("Version=1.0");

            //Last Update
            sw.WriteLine("");
            sw.WriteLine("[Update]");
            sw.WriteLine("; Handles last checked update");
            sw.WriteLine("Last_Update=" + DateTime.Now.ToString());  

            //Close INI
            sw.Close();
            /*ini.IniWriteValue("[Load]", "Auto_Del", "false");
            ini.IniWriteValue("[Load]", "Auto_Kill", "false");*/
        }
        else
        {
            m_path = path;

            string text = System.IO.File.ReadAllText(path);

            string[] lines =  System.IO.File.ReadAllLines(path);

            ArrayList file = new ArrayList();
            //string file = string.Empty;

            IniFile ini = new IniFile(path);

            if (!text.Contains("[Cache]"))
            {
                StreamWriter sw = new StreamWriter(path);
                foreach(string line in lines)
                    sw.WriteLine(line);
                //Client Caching
                sw.WriteLine("");
                sw.WriteLine("[Cache]");
                sw.WriteLine("; Handles Auto saving of product files");
                sw.WriteLine("Auto_Cache=false");
                sw.Close();
            }

            if (ini.IniReadValue("Auto", "Version") == string.Empty)
            {
                ini.IniWriteValue("Auto", "Version", "1.0");
            }
        }
    }
}
